# coding:utf-8
from bson import ObjectId
from flask import request, jsonify
from flask_restful import Resource
from app import api
from config import basepath
from sentiment import userDict


class NounDictController(Resource):
    # count
    def get(self):
        count = len(userDict)
        return {
            'code':0,
            'data':count
        }
    def post(self):
        name = request.form.get('name')
        if name in userDict:
            return {
                'code':1,
                'msg':'该名词已存在'
            }
        userDict.append(name)
        userdictPath = basepath + '/sentiment/senti_dict/dict/userdict.txt'
        open(userdictPath, 'a', encoding='utf-8').write(name+'\n')
        return {
            'code':0,
            'data':len(userDict)
        }

class NounsController(Resource):
    def get(self):
        return userDict[:min(50,len(userDict))]

# 展示前50个
# class NounsListController(Resource):
#     def get(self):
#         return userDict

api.add_resource(NounDictController, '/nounDict')
api.add_resource(NounsController, '/getNouns')